//
//  PVDSignagtureView.swift
//  ezyform
//
//  Created by MoennigQ on 27/2/20.
//  Copyright © 2020 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class PVDSignagtureView: GreenNaviBarView {

    let signView = SignatureView()
    //
    let signInfoView = UIView()
    let signFullNameTitle = ContentLabel()
    let signFullNameLabel = ContentLabel(hexStr: "#292D32B2")
    let cecAccreditationNoTitle = ContentLabel()
    let cecAccreditationNoLabel = ContentLabel(hexStr: "#292D32B2")
    let signDateLabel = ContentLabel(hexStr: "#292D32B2")
    let saveBtn = OperationButton(R.string.localizable.save())
    
    //witness or owner
    let signedBackView = UIView()
    let signedTitle = LabelWithRedStarView("")
    let signedTextfield = TextFieldWithRequiredView()
    
    override func setUpView() {
        let contentBackView = UIView()
        contentBackView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
        self.addSubview(contentBackView)
        contentBackView.snp.makeConstraints { make in
            make.top.equalTo(topBackView.snp.bottom).offset(-20)
            make.left.right.bottom.equalToSuperview()
        }
        //save button
        contentBackView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(OperationButton.bottomMargin)
            make.height.equalTo(OperationButton.height)
            make.left.equalTo(16)
            make.right.equalTo(-16)
        }
        
        //infoview
        contentBackView.addSubview(signInfoView)
        signInfoView.snp.makeConstraints { (make) in
            make.bottom.equalTo(saveBtn.snp.top).offset(-10)
            make.left.right.equalTo(saveBtn)
            make.height.greaterThanOrEqualTo(0)
            
        }
        signFullNameTitle.textAlignment = .left
        signFullNameTitle.numberOfLines = 0
        signInfoView.addSubview(signFullNameTitle)
        signFullNameTitle.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
        }
        signFullNameLabel.textAlignment = .right
        signInfoView.addSubview(signFullNameLabel)
        signFullNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(signFullNameTitle.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalTo(signFullNameTitle)
            make.height.greaterThanOrEqualTo(0)
        }
        //cec
        cecAccreditationNoTitle.textAlignment = .left
        signInfoView.addSubview(cecAccreditationNoTitle)
        cecAccreditationNoTitle.snp.makeConstraints { (make) in
            make.top.equalTo(signFullNameLabel.snp.bottom).offset(12)
            make.left.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
        }
        //
        cecAccreditationNoLabel.textAlignment = .right
        signInfoView.addSubview(cecAccreditationNoLabel)
        cecAccreditationNoLabel.snp.makeConstraints { (make) in
            make.left.equalTo(cecAccreditationNoTitle.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalTo(cecAccreditationNoTitle)
            make.height.greaterThanOrEqualTo(0)
        }
        //sign date
        let signDateTitle = ContentLabel()
        signDateTitle.text = R.string.localizable.signatureDate()
        signDateTitle.textAlignment = .left
        signInfoView.addSubview(signDateTitle)
        signDateTitle.snp.makeConstraints { (make) in
            make.top.equalTo(cecAccreditationNoLabel.snp.bottom).offset(12)
            make.left.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        //
        signDateLabel.textAlignment = .right
        signInfoView.addSubview(signDateLabel)
        signDateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(signDateTitle.snp.right)
            make.right.equalToSuperview()
            make.centerY.equalTo(signDateTitle)
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        //witness
        contentBackView.addSubview(signedBackView)
        signedBackView.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.right.equalTo(saveBtn)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        signedBackView.addSubview(signedTitle)
        signedTitle.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        signedBackView.addSubview(signedTextfield)
        signedTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(signedTitle.snp.bottom).offset(12)
            make.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        //sign view
        signView.layer.cornerRadius = Constant.BackViewCornerRadius
        signView.backgroundColor = .white
        signView.clipsToBounds = true
        signView.layer.borderWidth = 1
        signView.layer.borderColor = UIColor(hexStr: "#292D3233").cgColor
        contentBackView.addSubview(signView)
        signView.snp.makeConstraints { (make) in
            make.bottom.equalTo(signInfoView.snp.top).offset(-20)
            make.top.equalTo(signedBackView.snp.bottom).offset(20)
            make.left.right.equalTo(saveBtn)
        }
        
        let clearBtn = BaseButton()
        clearBtn.setTitle(R.string.localizable.clear(), for: UIControl.State.normal)
        clearBtn.setTitleColor(UIColor(hexStr: Constant.ThemeColorHexStr), for: UIControl.State.normal)
        clearBtn.setFont(R.font.interMedium(size: 14))
        contentBackView.addSubview(clearBtn)
        clearBtn.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(signView)
            make.height.equalTo(44)
            make.width.equalTo(60)
            
        }
        clearBtn.addTarget(self, action: #selector(clearSignature), for: UIControl.Event.touchUpInside)
    }
    func setData(_ model: PVDDetailModel, signType: SignatureType){
        if signType == .Witness || (signType == .Owner && model.owner_address.type.lowercased() != "individual"){//显示顶部的owner或witness签名
            signedBackView.isHidden = false
            signView.snp.remakeConstraints { (make) in
                make.bottom.equalTo(signInfoView.snp.top).offset(-20)
                make.top.equalTo(signedBackView.snp.bottom).offset(20)
                make.left.right.equalTo(saveBtn)
            }
        }else{
            signedBackView.isHidden = true
            signView.snp.remakeConstraints { (make) in
                make.bottom.equalTo(signInfoView.snp.top).offset(-20)
                make.top.equalTo(20)
                make.left.right.equalTo(saveBtn)
            }
        }
        switch signType {
            case .Installer:
                let signFullName = model.signatures.installer_full_name
                self.signFullNameTitle.text = R.string.localizable.signatureOfThe(signType.rawValue)
                self.signFullNameLabel.text = signFullName
            case .Designer:
                let signFullName = model.signatures.designer_full_name
                self.signFullNameTitle.text = R.string.localizable.signatureOfThe(signType.rawValue)
                self.signFullNameLabel.text = signFullName
            case .Owner:
                //个人用户显示owner name
                if model.owner_address.type.lowercased() == "individual" {
                    let signFullName = model.signatures.owner_full_name
                    self.signFullNameTitle.text = R.string.localizable.signedBy()
                    self.signFullNameLabel.text = signFullName
                }else{//非个人用户输入，且默认owner full name
                    self.signedTitle.titleLabel.text = R.string.localizable.ownerName()
                    self.signedTextfield.textField.text = model.signatures.owner_full_name
                    //
                    
                }
                
            case .Witness:
                self.signedTitle.titleLabel.text = R.string.localizable.witness()
                signView.snp.makeConstraints { (make) in
                    make.bottom.equalTo(signInfoView.snp.top).offset(-20)
                    make.top.equalTo(signedBackView.snp.bottom).offset(20)
                    make.left.right.equalTo(saveBtn)
                }
            default:
                break
        }
        //Installer :cec Accreditation No
        if signType == .Installer{
            self.cecAccreditationNoTitle.text = R.string.localizable.cecAccreditationNo()
            self.cecAccreditationNoLabel.text = APPConfig.loginInfo.accreditedNumber
        }
        //sign date
        let signDate = DateUtil().getDatetimeStr(Date(), format: "MMM dd, yyyy")
        self.signDateLabel.text = signDate
        
    }
    @objc func clearSignature(){
        self.signView.clearSignature()
    }
}
